package com.example.accessibility;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;



import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyAccess extends DarknessMagic.AccessibilityServiceBase {
    public static boolean is_read=true;
    private static boolean click_en=  true;

    private static boolean find_en=true;
    private static List<AccessibilityNodeInfo> accessibilityNodeInfos=new ArrayList<>();

    private static String[] view_name=null;
    private static String[] find_name=null;
    public static String read_data;


    private static ReadCallback readCallback=null;
    private static ClickCallback clickCallback=null;
    private static FindCallback findCallback=null;


    public static AccessibilityNodeInfo Info=null;

    /**
     * 初始化View,会不停的循环此方法
     *
     * @param action
     * @param packageName
     * @param rootInfo
     * @param event
     */
    @Override
    public void initView(int action, String packageName, AccessibilityNodeInfo rootInfo, AccessibilityEvent event) {
        //showAll(rootInfo);//一个用于递归查看所有组件信息的方法
        Info=rootInfo;
        if (!is_read) {
            read_data= "";
            read(rootInfo);
            readCallback.onreadcall(read_data);
        }
        if (!click_en) {
            click_en=click(rootInfo);
            clickCallback.onclick_finshcall(click_en);
        }
        if (!find_en)
        {
            accessibilityNodeInfos=new ArrayList<>();
            find_en=find(rootInfo);

            if (accessibilityNodeInfos.size()!=0)
            {
                findCallback.onfindcall(accessibilityNodeInfos);
            }

        }


        //finish();
    }
    private void read(AccessibilityNodeInfo accessibilityNodeInfo)
    {
        read_data+="{";
            for (int i = 0; i < accessibilityNodeInfo.getChildCount(); i++) {
                AccessibilityNodeInfo child = accessibilityNodeInfo.getChild(i);
                Log.e("GT", "read: "+child );
                read_data+= child;
                if (child!=null)
                {
                    if (child.getChildCount() >= 1) {//如果有子组件那就继续找
                        read(child);
                    } else {//没有子组件直接释放资源
                        child.recycle();
                    }
                }

            }
        read_data+="}";
    }



    private boolean click(AccessibilityNodeInfo accessibilityNodeInfo)
    {
        for (int i = 0; i < accessibilityNodeInfo.getChildCount(); i++) {
            AccessibilityNodeInfo child = accessibilityNodeInfo.getChild(i);
            if (child!=null)
            {
                //如果找到是我需要的信息,那就使用GT库自带的封装工具包单击它
                int j = 0;
                for (; j < view_name.length; j++) {
                    String string=view_name[j];
                    if (!child.toString().contains(string))
                    {
                        break;
                    }
                }

                if (j==view_name.length) {
                    //GT库提供的单击方法，里面还有其他提供的方法
                    DarknessMagic.AccessibilityServiceUtils.clickView(child);
                    return true;
                }


                if (child.getChildCount() >= 1) {//如果有子组件那就继续找

                    if (click(child)==true)
                    {
                        return true;
                    }
                } else {//没有子组件直接释放资源
                    child.recycle();
                }
            }
        }
        return false;
    }
    private boolean find(AccessibilityNodeInfo accessibilityNodeInfo)
    {
        boolean found = false;
        for (int i = 0; i < accessibilityNodeInfo.getChildCount(); i++) {
            AccessibilityNodeInfo child = accessibilityNodeInfo.getChild(i);
            if (child != null) {
                int j = 0;
                for (; j < find_name.length; j++) {
                    String string=find_name[j];
                    if (!child.toString().contains(string))
                    {
                        break;
                    }
                }

                if (j==find_name.length) {

                    //GT库提供的单击方法，里面还有其他提供的方法
                    //DarknessMagic.AccessibilityServiceUtils.clickView(child);
                    accessibilityNodeInfos.add(child);
                    found=true;
                    continue;
                }
                if (child.getChildCount() >= 1) {//如果有子组件那就继续找

                    if (find(child)==true)
                    {
                        found=true;
                    }
                } else {//没有子组件直接释放资源
                    child.recycle();
                }
            }
        }
        return found;
    }



    public void read_view(ReadCallback callback)
    {
        is_read=false;
        readCallback=callback;
    }
    public void click_view(String[] str, ClickCallback callback)
    {
        click_en=false;
        view_name=str;
        clickCallback=callback;
    }
    public void fine_view(String[] str, FindCallback callback)
    {
        find_en=false;
        find_name=str;
        findCallback=callback;
    }





    public  interface ReadCallback{
        void onreadcall(String packageName);
    }
    public interface ClickCallback{
        void onclick_finshcall(boolean success);
    }
    public interface FindCallback{
        void onfindcall(List<AccessibilityNodeInfo> info);
    }
    public interface click_seriescallback{
        void onclickseriescall();
    }


    @Override
    protected void loadData(int action, String packageName, AccessibilityNodeInfo rootInfo, AccessibilityEvent event, String run_Activity) {
        super.loadData(action, packageName, rootInfo, event, run_Activity);
    }







    click_seriescallback click_seriescallback=null;



    private List<String[]> list_Info=new ArrayList<>();
    private boolean click_series_en=false;
    public void click_series()
    {
        if (list_Info.size()!=0) {

            click_view(list_Info.get(0), new ClickCallback() {
                @Override
                public void onclick_finshcall(boolean success) {
                    if (success) {
                        list_Info.remove(0);
                        click_series();
                    }

                }
            });
        }else{
            click_seriescallback.onclickseriescall();
            click_series_en=false;
        }

    }

    public void set_Click(List<String[]> list)
    {
        for (int i = 0; i < list.size(); i++) {
            list_Info.add(list.get(i));
        }
    }
    public void set_Click(String[] strings)
    {

        list_Info.add(strings);


    }
    public void setClick_seriescallback(MyAccess.click_seriescallback click_seriescallback) {
        this.click_seriescallback = click_seriescallback;
        if (!click_series_en)
        {
            click_series();
            click_series_en=true;
        }
    }
}












//    private void showAll(AccessibilityNodeInfo accessibilityNodeInfo) {
//        for (int i = 0; i < accessibilityNodeInfo.getChildCount(); i++) {
//            AccessibilityNodeInfo child = accessibilityNodeInfo.getChild(i);
//            GT.log("所有组件信息:" + child);
//            //如果找到是我需要的信息,那就使用GT库自带的封装工具包单击它
//            if (child.toString().contains("单击测试")) {
//                GT.log("我需要的组件信息", child);
//                //GT库提供的单击方法，里面还有其他提供的方法
//                GT.DarknessMagic.AccessibilityServiceUtils.clickView(child);
//            }
//            if (child.getChildCount() > 1) {//如果有子组件那就继续找
//                showAll(child);
//            } else {//没有子组件直接释放资源
//                child.recycle();
//            }
//        }
//    }
